<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<%@ taglib prefix="his_fns" uri="/WEB-INF/tlds/his_fns.tld" %>
<script src="${ctxStatic}/jquery/jquery.form.js"></script>

<div class="app">
    <section class="layout">
        <section class="main-content col-lg-12" id="expExportDiv">
            <div class="content-wrap">
                <div class="wrapper" style="bottom: 50px;">
                    <ul class="nav nav-tabs">
                        <li class="active">
                            <a href="#">申请出库</a>
                        </li>
                    </ul>
                    <section class="panel panel-default">
                        <div class="panel-body">
                            <div class="table-responsive no-border">
                                <div class="datatable-top">
                                    <div class="pull-left">
                                        <form:form id="searchForm" modelAttribute="expProvideApplication"
                                                   onsubmit="return loadDivForm('mainCenterDiv','searchForm','${ctx}/csm/expProvideApplication/query');"
                                                   method="post" class="form-inline">
                                            <div class="form-group">
                                                <label class=" control-label">出库类别：</label>
                                                <select class="form-control" id="exportClass"><option>发放</option></select>
                                            </div>
                                            <div class="form-group">
                                                <label class=" control-label">出库日期：</label>
                                                <input name="importDate" id="exportDate" htmlEscape="false" class="form-control Wdate" data-parsley-required="true" readonly/>
                                            </div>
                                            <div class="form-group">
                                                <label class=" control-label">申请单位：</label>
                                                <form:input type="hidden" path="storageIn" id="storageId" htmlEscape="false" class="form-control" maxlength="64" />
                                                <form:input path="storageName" id="storageName" htmlEscape="false" class="form-control"/>
                                            </div>
                                            <div class="form-group">
                                                <input id="btnSubmit" class="btn btn-primary" type="button" onclick="saveExport()" value="保存" />
                                            </div>
                                        </form:form>
                                    </div>
                                </div>
                                <form id="saveListForm" action="${ctx}/csm/expProvideApplication/saveProvideApplication" method="post" class="form-inline">
                                <table id="contentTable" class="table table-bordered table-striped mg-t datatable">
                                    <thead>
                                    <tr>
                                        <th hidden>jsonText</th>
                                        <th hidden>id</th>
                                        <th hidden>infoId</th>
                                        <th hidden>请领单位</th>
                                        <th hidden>发放单位</th>
                                        <th>编码</th>
                                        <th>名称</th>
                                        <th hidden>规格</th>
                                        <th hidden>单位</th>
                                        <th hidden>包装数量</th>
                                        <th>规格</th>
                                        <th>单位</th>
                                        <th>厂家</th>
                                        <th hidden>组织机构ID</th>
                                        <th>当前库存</th>
                                        <th>批号</th>
                                        <th>出库数量</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <c:forEach items="${list}" var="expProvideApplication" varStatus="idx">
                                    <tr tbIdx="${idx.index}">
                                        <td hidden>${expProvideApplication.id}<input type="hidden" id="id${idx.index}" name="expProvideApplicationList[${idx.index}].id"
                                                                                          value="${expProvideApplication.id}" /></td>
                                        <td hidden>${expProvideApplication.infoId}<input type="hidden" id="infoId${idx.index}" name="expProvideApplicationList[${idx.index}].infoId"
                                                                                      value="${expProvideApplication.infoId}" /></td>
                                        <td hidden>${expProvideApplication.storageIn}<input type="hidden" id="storageIn${idx.index}" name="expProvideApplicationList[${idx.index}].storageIn"
                                                                                      value="${expProvideApplication.storageIn}" /></td>
                                        <td hidden>${expProvideApplication.storageOut}<input type="hidden" id="storageOut${idx.index}" name="expProvideApplicationList[${idx.index}].storageOut"
                                                                                             value="${expProvideApplication.storageOut}" /></td>
                                        <td>${expProvideApplication.expCode}<input type="hidden" name="expProvideApplicationList[${idx.index}].expCode" value="${expProvideApplication.expCode}" /></td>
                                        <td>${expProvideApplication.expName}<input type="hidden"  name="expProvideApplicationList[${idx.index}].expName"
                                                                                                        value="${expProvideApplication.expName}"/></td>
                                        <td hidden>${expProvideApplication.minSpec}<input type="hidden" name="expProvideApplicationList[${idx.index}].minSpec"
                                                                                          value="${expProvideApplication.minSpec}"/></td>
                                        <td hidden>${fns:getDictLabel(expProvideApplication.minUnits,'SPEC_UNIT','')}<input type="hidden" name="expProvideApplicationList[${idx.index}].minUnits"
                                                value="${expProvideApplication.minUnits}"/></td>
                                        <td hidden>${expProvideApplication.packageNum}<input type="hidden" name="expProvideApplicationList[${idx.index}].packageNum"
                                                                                       value="${expProvideApplication.packageNum}"/></td>
                                        <td>${expProvideApplication.packageSpec}<input type="hidden" name="expProvideApplicationList[${idx.index}].packageSpec"
                                                                                       value="${expProvideApplication.packageSpec}"/></td>
                                        <td>${fns:getDictLabel(expProvideApplication.packageUnits,'SPEC_UNIT','')}<input type="hidden"
                                               name="expProvideApplicationList[${idx.index}].packageUnits" value="${expProvideApplication.packageUnits}"/></td>
                                        <td>${his_fns:getExpSupplierById(expProvideApplication.expInfo.expSupplierCata.id)}
                                            <input type="hidden" name="expProvideApplicationList[${idx.index}].expInfo.expSupplierCata.id"
                                                                                          value="${expProvideApplication.expInfo.expSupplierCata.id}"/></td>
                                        <td hidden>${expProvideApplication.orgId}<input type="hidden" id="orgId${idx.index}" name="expProvideApplicationList[${idx.index}].orgId"
                                                                                      value="${expProvideApplication.orgId}" /></td>
                                        <td>${expProvideApplication.expStock.quantity}<input type="hidden" id="stockQuantity${idx.index}"
                                                                                               name="expProvideApplicationList[${idx.index}].expStock.quantity"
                                                                                           value="${expProvideApplication.expStock.quantity}"/></td>
                                        <td>${expProvideApplication.expStock.batchNo}<input type="hidden" name="expProvideApplicationList[${idx.index}].expStock.batchNo"
                                                                                       value="${expProvideApplication.expStock.batchNo}" /></td>
                                        <td><input name="expProvideApplicationList[${idx.index}].quantityOut" id="quantity${idx.index}"
                                                                                     value="${expProvideApplication.quantity}" class="form-control" width="20px"
                                                   onchange="changeFun('quantity','${idx.index}')"></td>
                                        <td>
                                            <a href="#" class="applyBt btn btn-danger btn-xs" onclick="removeList(${idx.index},this)">删除</a>
                                        </td>
                                    </tr>
                                    </c:forEach>
                                </table>
                                </form>
                            </div>
                        </div>
                    </section>
                </div>
            </div>
        </section>
    </section>
</div>
<script type="text/javascript">
    addExportDate();
    //设置入库日期默认当前时间
    function addExportDate(){
        var date = new Date();
        var nowMonth = "";
        if (date.getMonth() + 1 <= 9) {
            nowMonth = "0" + (date.getMonth() + 1) + "";
        } else {
            nowMonth = (date.getMonth() + 1) + "";
        }
        var hours = "";
        if (date.getHours() < 10) {
            hours = "0" + date.getHours();
        } else {
            hours = date.getHours();
        }
        var minutes = "";
        if (date.getMinutes() < 10) {
            minutes = "0" + date.getMinutes();
        } else {
            minutes = date.getMinutes();
        }
        var seconds = "";
        if (date.getSeconds() < 10) {
            seconds = "0" + date.getSeconds();
        } else {
            seconds = date.getSeconds();
        }
        var now = date.getFullYear() + "-" + nowMonth + "-" + date.getDate() + " " + hours + ":" + minutes + ":" + seconds;
        $('#exportDate').val(now);
    }

    /*申请单位补全*/
    autoCompleteFirm("storageName");
    function autoCompleteFirm(inputId) {
        var url = "${ctx}/csm/expStorageDept/autoComplete";
        autoComplete(inputId, url,
                function (data) {
                    var rows = [];
                    for (var i = 0; i < data.length; i++) {
                        rows[rows.length] = {
                            data: data[i],
                            value: data[i].id,
                            result: data[i].storageName
                        };
                    }
                    return rows;
                }, function (data, i, max) {
                    return data.storageName;
                }, function (event, data, formatted) {
                    $('#storageName').attr('value', data.storageName);
                    $('#storageId').attr('value', data.storageCode);
                    $('#searchForm').submit();
                });
    }
    //判断出库数量是否大于当前库存
    function changeFun(inputId, idx) {
        var num = Number($.trim($("#" + inputId + idx).val()));
        var stockQuantity = Number($.trim($("#stockQuantity" + idx).val()));
        if(num > stockQuantity){
            toastr.error('库存数量不够出库,请重新输入出库数量!');
            $("#" + inputId + idx).val($("#stockQuantity" + idx).val());
            return false;
        }
    }

    function validate(){
        var flag = true;
        $("#contentTable tbody tr").each(function () {
            var tbIdx = $(this).attr("tbIdx");
            var num = Number($('#quantity' + tbIdx).val());
            var stockQuantity = Number($('#stockQuantity' + tbIdx).val());
            if (num > stockQuantity) {
                toastr.error('第'+(Number(tbIdx)+1)+'行库存数量不够出库,请重新输入出库数量!');
                flag = false;
                return false;
            }else{
                flag = true;
                return true;
            }
        });
        return flag;
    }

    //出库保存
    function saveExport() {
        if(validate() == true){
            $("#saveListForm").ajaxSubmit({
                success: function (data) {
                    if (data.code == 'success') {
                        toastr.success(data.data);
                    } else {
                        toastr.error(data.data);
                    }
                },
                error: function (data) {
                    toastr.error(data.data);
                }
            });
        }else{
        }
    }
</script>